﻿using System.ComponentModel;
using System.Web.Services;
using Google.Translates.Core;
using Google.Translates.WebService.Properties;
using Google.Translates.WebService.Rules;
using Google.Translates.WebService.TranslateParmas;
using Google.Translates.WebService.TranslateResults;

namespace Google.Translates.WebService
{
    /// <summary>
    /// Summary description for DoTranslates
    /// </summary>
    [WebService(Namespace = "http://microsoft.com/webservices/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
    public class DoTranslates : System.Web.Services.WebService
    {
        private const string ERROR_MSG_FAIL = "Translation is fail";

        /// <summary>
        /// 實現TextTransmission(Type 1#) 方式的 Translate 應用
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [WebMethod]
        public TextTransmissionResult TranslateByTextTramsmission(TextTransmissionParma input)
        {
            var objRule = new TextTransmissionRule();
            var result = new TextTransmissionResult
                             {
                                 TargetLanguageKey = input.TargetLanguageCode,
                                 ErrorMessage = objRule.CheckInputParmaIsUseful(input)
                             };
            if (result.ErrorMessage.Length > 0)
                return result;
            string sourceLanguageCode = input.SourceLanguageCode;
            string translateText;
            if (string.IsNullOrEmpty(sourceLanguageCode))
                translateText = new TranslatorByGoogleHttp().GetTranslationsByLongSentence(input.SourceText, "",
                                                                                           input.TargetLanguageCode,
                                                                                           ref sourceLanguageCode, null);
            else
                translateText = new TranslatorByGoogleHttp().GetTranslationsByLongSentence(input.SourceText,
                                                                                           sourceLanguageCode,
                                                                                           input.TargetLanguageCode);
            if (string.IsNullOrEmpty(translateText))
                result.ErrorMessage = ERROR_MSG_FAIL;
            else
            {
                result.SourceLanguageKey = sourceLanguageCode;
                result.TranslateText = translateText;
            }
            return result;
        }

        /// <summary>
        /// 獲取 Engine 用於保存SourceContextFile的Share Folder
        /// 用於協助 Service [TranslateByFileTransmission] 的應用
        /// 因為請求服務執行的Application要先獲取Share Folder Path, 預先把 Source Context File 存放到 Google Translate Engine Service 指定的 Share Folder 中
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public string GetShareFolderPathBySourceContext()
        {
            return Settings.Default.Default_FileTransmission_AccessShareFolderDir;
        }

        /// <summary>
        /// 實現FileTranslation (Type 1#b) 方式的 Translate 應用
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [WebMethod]
        public FileTransmissionResult TranslateByFileTransmission(FileTransmissionParma input)
        {
            return null;
        }

        /// <summary>
        /// 實現Database Update(Type 2#) 方式的 Translate 應用
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [WebMethod]
        public DbUpdateResult TranslateByDbUpdate(DbUpdateParma input)
        {
            return null;
        }
    }
}
